define(['./BaseView', '../models/myAmendView','../libs/extend', 'underscore'], function(BaseView, myAmendView, extend, _) {
    return BaseView.extend({
        themeData:null,
        events: {
            "click #giveupData": "giveupData",
            "click #saveData": "saveData",
            "focus #beginTime": "beginTime",
            "focus #endTime": "endTime",
            "focus #sumDate": "sumDate",
            "focus #acceptDate": "acceptDate",
            "blur .my-sum": "toFixed",
            // "blur .my-costMoney": "toFixed_C" ,
            "click .clearData_A" :"clearData_A",
            "click .clearData_B" :"clearData_B"
        },
        //初始化
        initialize: function(){
            BaseView.prototype.initialize.apply(this, arguments);
            var content = $('<ul class="myAmend"></ul>');
            this.$el.append(content);
            this.themeData = new myAmendView();
            this.listenTo(this.themeData, 'change', this.onChange);
        },
        render:function(){
            var data = this.themeData.toJSON();
            this.createMain(data);
            return this;
        },
        //重新render
        onChange: function(){
            this.render();
        },
        //起始时间
        beginTime:function(){
            extend.setSessionStorage('Ykey',1);
            var opt_date = {
                preset: 'date', //日期date
                theme: 'default', //皮肤样式
                dateFormat: 'yy-mm-dd', // 日期格式
                setText: '确定', //确认按钮名称
                cancelText: '取消', //取消按钮
                dateOrder: 'yymmdd', //面板中日期排列格式
                dayText: '日', monthText: '月', yearText: '年' //面板中年月日文字
            };
            this.$el.find('#beginTime').mobiscroll(opt_date)  ;
            $('#beginTime').blur();
        },
        //结束时间
        endTime:function(){
            extend.setSessionStorage('Ykey',2);
            var opt_date = {
                preset: 'date', //日期date
                theme: 'default', //皮肤样式
                dateFormat: 'yy-mm-dd', // 日期格式
                setText: '确定', //确认按钮名称
                cancelText: '取消', //取消按钮
                dateOrder: 'yymmdd', //面板中日期排列格式
                dayText: '日', monthText: '月', yearText: '年', //面板中年月日文字
            };
            this.$el.find('#endTime').mobiscroll(opt_date) ;
            $('#endTime').blur();
        },
        //预期收款时间
        sumDate:function(){
            var opt_date = {
                preset: 'date', //日期date
                theme: 'default', //皮肤样式
                dateFormat: 'yy-mm-dd', // 日期格式
                setText: '确定', //确认按钮名称
                cancelText: '取消', //取消按钮
                dateOrder: 'yymmdd', //面板中日期排列格式
                dayText: '日', monthText: '月', yearText: '年', //面板中年月日文字
            };
            this.$el.find('#sumDate').mobiscroll(opt_date) ;
            $('#sumDate').blur();
        },
        //验收时间
        acceptDate:function(){
            var opt_date = {
                preset: 'date', //日期date
                theme: 'default', //皮肤样式
                dateFormat: 'yy-mm-dd', // 日期格式
                setText: '确定', //确认按钮名称
                cancelText: '取消', //取消按钮
                dateOrder: 'yymmdd', //面板中日期排列格式
                dayText: '日', monthText: '月', yearText: '年', //面板中年月日文字
            };
            this.$el.find('#acceptDate').mobiscroll(opt_date) ;
            $('#acceptDate').blur();
        },
        //保留两位小数
        // toFixed:function () {
        //     var num = /^[0-9]+(\.[0-9]+)?$/;
        //     if($('.my-sum').val() != '' && num.test($('.my-sum').val())){
        //         var b = parseFloat($('.my-sum').val()).toFixed(2);
        //         $('.my-sum').val(b)
        //     }
        // },
        // toFixed_C:function () {
        //     var num = /^[0-9]+(\.[0-9]+)?$/;
        //     if($('.my-costMoney').val() != '' && num.test($('.my-costMoney').val())){
        //         var b = parseFloat($('.my-costMoney').val()).toFixed(2);
        //         $('.my-costMoney').val(b)
        //     }
        // },
        clearData_A:function () {
            this.$el.find('.my-sumDate').val('')
        },
        clearData_B:function () {
            this.$el.find('.my-acceptDate').val('')
        },
        createMain:function(data){
            var title = '';
            var dataId = data.pk_record;//事物id
            var departments = data.deptname;//部门名称
            var username = data.username;//人员
            var project = data.project;//项目
            var reason = data.reason;//事由
            var projecttype = data.projecttype;//项目类型
            var province = data.province;//所属省区
            var branchcomp  = data.branchcomp ;//分公司
            var begindate = data.begindate;//起始日期
            var enddate = data.enddate;//结束日期
            var daysnum = data.daysnum;//天数
            var period = data.period;//季度
            var achievement = data.achievement;//阶段成果
            var manager = data.manager;//项目负责人
            var phone = data.phone;//项目负责人电话
            var projstatus = data.projstatus;//项目状态
            var iscollected = data.iscollected.value;//是否涉及收款
            var collectedamount = data.collectedamount;//收款金额
            var collectedtime = data.collectedtime;//收款时间
            var isinspected = data.isinspected.value;//是否涉及验收
            var inspecttime = data.inspecttime;//验收时间
            var expensetype = data.expensetype;//费用自理方式
            var expenseamount = data.expenseamount;//费用自理金额
            var remarks = data.remarks;//备注
            var attachments = data.attachments;//附件
            if(phone == '无'){
                phone = '';
            }
            var contentItem = [
                '<p>'+ title +'</p>',
                '<li><span class="item">公司：</span><span class="my-company">用友网络科技股份有限公司</span></li>',
                '<li><span class="item">部门：</span><span class="my-departments">'+ departments +'</span></li>',
                '<li><span class="item">人员：</span><span class="my-name">'+ username +'</span></li>',
                '<li><span class="item">项目：</span><i>*必填</i><input class="my-item" type="text" placeholder="最多20字" value="'+ project +'"></li>',
                '<li><span class="item">事由：</span><textarea placeholder="最多100字" class="my-cause">'+ reason +'</textarea></li>',
                '<li><span>所属类型：</span> <select name="" id="my-type" class="my-type"><option value="现场支持">现场支持</option><option value="远程支持">远程支持</option><option value="软件园现场">软件园现场</option><option value="调研">调研</option><option value="售前支持">售前支持</option><option value="实施支持">实施支持</option><option value="其他">其他</option></select></li>',
                '<li><span>所属省区：</span>',
                '<select name="" id="my-province" class="my-province">',
                '<option>北京</option>',
                '<option>天津</option>',
                '<option>上海</option>',
                '<option>重庆</option>',
                '<option>河北</option>',
                '<option>山西</option>',
                '<option>辽宁</option>',
                '<option>吉林</option>',
                '<option>黑龙江</option>',
                '<option>江苏</option>',
                '<option>浙江</option>',
                '<option>安徽</option>',
                '<option>福建</option>',
                '<option>江西</option>',
                '<option>山东</option>',
                '<option>河南</option>',
                '<option>湖北</option>',
                '<option>湖南</option>',
                '<option>广东</option>',
                '<option>海南</option>',
                '<option>四川</option>',
                '<option>贵州</option>',
                '<option>云南</option>',
                '<option>陕西</option>',
                '<option>甘肃</option>',
                '<option>青海</option>',
                '<option>台湾</option>',
                '<option>内蒙古</option>',
                '<option>广西</option>',
                '<option>西藏</option>',
                '<option>宁夏</option>',
                '<option>新疆</option>',
                '<option>香港</option>',
                '<option>澳门</option>',
                '</select><i>*必填</i>',
                '<li><span>项目所属分公司：</span><input class="my-filiale" placeholder="最多20字" type="text" value="'+ branchcomp +'"></li>',
                '<li><span class="item">日期起：</span><i>*必填</i><input class="date-begin" id="beginTime" autocomplete="off" type="text" value="'+begindate+'"></li>',
                '<li><span class="item">日期止：</span><input class="date-end" id="endTime" autocomplete="off" type="text" value="'+enddate+'"></li>',
                '<li><span class="item">天数：</span><span class="my-dataNum">'+ daysnum +'</span></li>',
                '<li><span class="item">期间：</span><span class="my-season">'+ period +'</span></li>',
                '<li><span class="intype">阶段成果：</span><textarea placeholder="最多100字" class="my-resault">'+ achievement +'</textarea></li>',
                '<li><span>项目负责人：</span><input placeholder="最多20字" class="my-principal" type="tel" value="'+manager+'"></li>',
                '<li><span>项目负责人电话：</span><input class="my-principaltel" type="tel" value="'+phone+'"></li>',
                '<li><span>项目状态：</span>',
                '<select name="" id="my-state" class="my-state">',
                '<option>售前</option>',
                '<option>实施</option>',
                '<option>维护</option>',
                '</select>',
                '</li>',
                '<li><span class="intype_1">涉及收款：</span>' ,
                ' <p><input type="radio" class="my-gathering" name="checkit" value="true"><span class="choose">是</span></p>',
                '<p><input type="radio" class="my-gathering" name="checkit" value="false"><span class="choose">否</span></p>',
               '</li>',
                '<li><span class="item_A">预期收款金额：</span><a class="cor">￥</a><input class="my-sum" type="text" value="'+collectedamount+'"></li>',
                '<li><span>预期收款时间：</span><input class="my-sumDate" id="sumDate"  autocomplete="off" type="text" value="'+collectedtime+'"><span class="clearData_A">×</span></li>',
                '<li><span class="intype_1">涉及验收：</span>',
                '<p><input type="radio" class="my-accept" name="check" value="true"><span class="choose">是</span></p>',
                '<p><input type="radio" class="my-accept" name="check" value="false"><span class="choose">否</span></p>',
                '</li>',
                '<li><span>验收时间：</span><input class="my-acceptDate" id="acceptDate" autocomplete="off" type="text" value="'+inspecttime+'"><span class="clearData_A">×</span></li>',
                '<li><span class="intype_1">费用自理：</span>',
                '<p><input type="radio" class="my-intype" name="cost" value="是"><span class="choose">是</span></p>',
            '<p><input type="radio" class="my-intype" name="cost" value="否"><span class="choose">否</span></p>',
            '<p><input type="radio" class="my-intype" name="cost" value="部分自理"><span class="choose">部分自理</span></p>',
                '</li>',
                '<li><span>费用金额：</span><a class="cor">￥</a><input class="my-costMoney" type="text" value="'+expenseamount+'"></li>',
                '<li><span class="intype item">备注：</span><textarea placeholder="最多100字"class="my-remark">'+remarks+'</textarea></li>',
                '<li class="accessary"><span>附件：</span></li>',
                '<li>',
                '<input type="button" value="返回" id="giveupData"/>',
                '<input type="button" value="保存" id="saveData"/>',
                '</li>'
            ].join('');
            this.$el.find('.myAmend').html(contentItem);
            //附件循环加载
            if(attachments.length==0){
                var  mainspan = '<span class="main-attachment">暂无附件</span>';
                this.$el.find('.accessary').append(mainspan)
            }else{
                for(var i=0;i<attachments.length;i++){
                    mainspan = '<span class="main-attachment">'+ attachments[i] +'</span>';
                    this.$el.find('.accessary').append(mainspan)
                }
            }

            this.$el.find('#my-type option[value = "'+ projecttype +'"]').attr("selected",true);

            this.$el.find('#my-province option[value = "'+ province +'"]').attr("selected",true);

            this.$el.find('#my-state option[value = "'+ projstatus +'"]').attr("selected",true);

            this.$el.find('input[name=checkit][value="'+iscollected+'"]').attr("checked",true);

            this.$el.find('input[name=check][value="'+isinspected+'"]').attr("checked",true);

            this.$el.find('input[name=cost][value="'+expensetype+'"]').attr("checked",true);
            this.inputListener();
        },

        //取消按钮
        giveupData: function () {
            Backbone.history.navigate('#OwnParticular', {
                trigger: true
            });
        },
        //监听输入字数限制
        inputListener:function(){
            this.$el.find('.my-item').bind('input propertychange',function(){//项目
                var inputLength =  $(this).val().length;
                if(inputLength>=20){
                    layer.alert('最多输入20字');
                    $(this).blur() ;
                    return false
                }
            });
            this.$el.find('.my-cause').bind('input propertychange',function(){//事由
                var inputLength =  $(this).val().length;
                if(inputLength>=100){
                    layer.alert('最多输入100字');
                    $(this).blur() ;
                    return false
                }
            });
            this.$el.find('.my-filiale').bind('input propertychange',function(){//分公司
                var inputLength =  $(this).val().length;
                if(inputLength>=20){
                    layer.alert('最多输入20字');
                    $(this).blur() ;
                    return false
                }
            });
            this.$el.find('.my-resault').bind('input propertychange',function(){ //阶段成果
                var inputLength =  $(this).val().length;
                if(inputLength>=100){
                    layer.alert('最多输入100字');
                    $(this).blur() ;
                    return false
                }
            });
            this.$el.find('.my-principal').bind('input propertychange',function(){ //负责人
                var inputLength =  $(this).val().length;
                if(inputLength>=20){
                    layer.alert('最多输入20字');
                    $(this).blur() ;
                    return false
                }
            });
            this.$el.find('.my-remark').bind('input propertychange',function(){ //备注
                var inputLength =  $(this).val().length;
                if(inputLength>=100){
                    layer.alert('最多输入100字');
                    $(this).blur() ;
                    return false
                }
            });

        },
         //保存按钮
        saveData: function () {
            var startTime =  new Date($('#beginTime').val());
            var endTime = new Date($('#endTime').val());
            if(endTime<startTime){
                layer.alert('结束时间不能小于起始时间');
                return false
            }
            var tel = /^1[0-9]{10}$/;
            var phoneType = /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/;
            var num = /^[0-9]+(\.[0-9]+)?$/;
            //输入校验
            if (!(tel.test($('.my-principaltel').val()) || phoneType.test($('.my-principaltel').val())) && $('.my-principaltel').val() != '') {
                layer.alert('号码格式不正确')  ;
                $('.my-principaltel').val('');
                return false
            }
            if($('.my-sum').val() != '' && !(num.test($('.my-sum').val()))){
                layer.alert('请输入整数或小数') ;
                $('.my-sum').val('');
                return false
            }
            if($('.my-costMoney').val() != '' && !(num.test($('.my-costMoney').val()))){
                layer.alert('请输入整数或小数') ;
                $('.my-costMoney').val('');
                return false
            }
            if($('.my-item').val().length>20){
                layer.alert('项目最多输入20字');
                return false
            }
            if($('.my-cause').val().length>100){
                layer.alert('事由最多输入100字');
                return false
            }
            if($('.my-filiale').val().length>20){
                layer.alert('分公司最多输入20字');
                return false
            }
            if($('.my-resault').val().length>100){
                layer.alert('阶段成果最多输入100字');
                return false
            }
            if($('.my-principal').val().length>20){
                layer.alert('项目负责人最多输入20字');
                return false
            }
            if($('.my-remark').val().length>100){
                layer.alert('备注最多输入100字');
                return false
            }
            var pk_record = extend.getlocalStorage('YY_themsID');
            var company = $('.my-company').html();//公司
            var deptname = $('.my-departments').html();//部门名称
            var pk_user = extend.getlocalStorage('YY_userInfo').userId;//人员id
            var username = $('.my-name').html();//人员姓名
            var project = this.$el.find('.my-item').val();//项目
            var reason = $('.my-cause').val();//事由
            var projecttype = $('.my-type').val();//项目类型
            var province = $('.my-province').val();//所属省区
            var branchcomp = $('.my-filiale').val();//分公司
            var begindate = $('.date-begin').val();//起止日期
            var enddate = $('.date-end').val();//结束日期
            var daysnum = $('.my-dataNum').html();//天数
            var period = $('.my-season').html();//季度
            var achievement = $('.my-resault').val();//阶段成果
            var manager = $('.my-principal').val();//项目负责人
            var phone = $('.my-principaltel').val();//负责人电话
            var projstatus = $('.my-state').val();//项目状态
            if($("input[name='checkit']:checked").val()){//涉及收款
                iscollected = $("input[name='checkit']:checked").val();
            }else{
                iscollected = '';
            }
            var collectedamount = $('.my-sum').val();//收款金额
            var collectedtime = $('.my-sumDate').val();//收款时间
            if($("input[name='check']:checked").val()){
                var isinspected = $("input[name='check']:checked").val();//涉及验收？
            }else{
                isinspected = '';
            }
            var inspecttime = $('.my-acceptDate').val();//验收时间
            if($("input[name='cost']:checked").val()){
                var expensetype = $("input[name='cost']:checked").val();//费用自理方式
            }else{
                expensetype = '';
            }
            var expenseamount = $('.my-costMoney').val();//费用自理金额
            var remarks = $('.my-remark').val();//备注


            var NewData = {
                "pk_record":pk_record,
                "company": company,
                "deptname": deptname,
                "pk_user": pk_user,
                "username": username,
                "project": project,
                "reason": reason,
                "projecttype": projecttype,
                "province": province,
                "branchcomp": branchcomp,
                "begindate": begindate,
                "enddate": enddate,
                "daysnum": daysnum,
                "period": period,
                "achievement": achievement,
                "manager": manager,
                "phone": phone,
                "projstatus": projstatus,
                "iscollected": iscollected,
                "collectedamount": collectedamount,
                "collectedtime": collectedtime,
                "isinspected": isinspected,
                "inspecttime": inspecttime,
                "expensetype": expensetype,
                "expenseamount": expenseamount,
                "remarks": remarks
            };
            console.log(NewData)
            if(project!='' && begindate!='' && province!=''){
                extend.sendData(window.app.proxy.root + 'editResearchRecord', NewData, function(res){
                    if(res.status == 'success'){
                        layer.alert('保存成功',function (index) {
                            Backbone.history.history.back();
                            layer.close(index)
                        });

                    }else{
                        extend.Toast(res.message,2000);
                    }
                },function(e){
                    extend.Toast('网络错误',2000);
                });
            }else{
                extend.Toast('您有必填项未填写',2000);
            }
        }
    });
});

